package edu.ccut.saturn.component.loader;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.IDirectoryInfo;
import edu.ccut.saturn.component.ILogger;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.SaturnLoggerManager;
import edu.ccut.saturn.component.impl.directory.SaturnDirectoryFactory;
import edu.ccut.saturn.component.xml.Dom4jUtils;

/**
 * 文件夹路径信息的加载类。路径信息描述了文件夹代表的路径资源的类型、版本、创建时间、描述信息等。这些信息记录在了文件夹下的“.saturn”文件中。
 * 
 * @author 谷钰
 * 
 * @version 1.0 2008.9.03
 * 
 * @see IDirectoryInfo
 * 
 */
public abstract class DirectoryInfoLoader {

	private static final String LOGGER_KEY_LOAD_BUNDLE = SaturnLoggerManager.LOGGER_KEY_LOAD_BUNDLE;

	private static final ILogger logger = SaturnLoggerManager.getInstance()
			.getSaturnLogger(LOGGER_KEY_LOAD_BUNDLE);

	private static final String KEY_VERSION = "version";

	private static final String KEY_DESCRIPTION = "description";

	private static final String KEY_TYPE = "type";

	private static final String KEY_TIME = "time";

	/**
	 * 加载描述文件夹的路径资源信息。并设置路径资源的在bundle中的相对路径。
	 * 
	 * @param bundle
	 * @param filePath
	 * @return
	 * @throws SaturnComponentException
	 */
	public static IDirectoryInfo loadDirectoryInfo(Bundle bundle,
			String filePath) throws SaturnComponentException {

		InputStream in = null;

		try {
			URL url = bundle.getEntry(filePath);

			if (url == null) {
				logger.error("can't find file in " + bundle.getSymbolicName()
						+ "with name " + filePath);

				throw new SaturnComponentException("can't find file in "
						+ bundle.getSymbolicName() + "with name " + filePath);
			}
			in = url.openStream();
			IDirectoryInfo directoryInfo = loadDirectoryInfo(in);
			
			if (directoryInfo != null) {
				String directoryPath = filePath.substring(0, filePath
					.lastIndexOf("/"));

				logger
					.debug("  RootDirectory path : \"" + directoryPath
							+ "\"");

				logger.debug("");
				directoryInfo.setDirectoryPath(directoryPath);
			}
			return directoryInfo;

		} catch (IOException e) {
			e.printStackTrace();

		} finally {

			try {

				if (in != null) {
					in.close();
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static IDirectoryInfo loadDirectoryInfo(InputStream in)
			throws SaturnComponentException {

		Document document = Dom4jUtils.getDocment(in);
		Element root = document.getRootElement();
		String version = root.elementTextTrim(KEY_VERSION);
		String description = root.elementTextTrim(KEY_DESCRIPTION);
		String type = root.elementTextTrim(KEY_TYPE);
		String time = root.elementTextTrim(KEY_TIME);
		logger.debug("  info : { version : '" + version + "', '" + 
				                "description : '" + description + "', '" + 
				                "type : '" + type + "', '" + 
				                "time : '" + time + "'}");
		
		IDirectoryInfo directoryInfo = SaturnDirectoryFactory.getInstance()
				.createDirectory(version, description, type, time);

		return directoryInfo;
	}
}
